package com.widget.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.heartware.sh.blackhole.R;

public class CommonDialogFragment extends DialogFragment {

	private static int width;
	
	private static int height;
	
	private Button cancel;

	private Button confirm;
	
	private TextView title;

	private TextView content;
	
	private String titleText;
	
	private String contentText;
	
	private String leftBtnName;
	
	private String rightBtnName;
	
	private static onDialogButtonClickListener buttonOnClickListener;

	/**
	 * @param title 标题
	 * @param content 正文
	 * @param leftBtnName 左按钮
	 * @param righBtntName 右按钮
	 * @return
	 */
	public static CommonDialogFragment newInstance(String title, String content, String leftBtnName, String righBtntName) {
		CommonDialogFragment dialogFrag = new CommonDialogFragment();
		Bundle args = new Bundle();
		args.putString("titleText", title);
		args.putString("contentText", content);
		args.putString("leftBtnName", leftBtnName);
		args.putString("rightBtnName", righBtntName);
		dialogFrag.setArguments(args);
		
		return dialogFrag;
	}

	public static interface onDialogButtonClickListener {
		public void confrimButtonClcik();

		public void cancelButtonClick();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		titleText = getArguments().getString("titleText");
		
		contentText = getArguments().getString("contentText");
		
		leftBtnName = getArguments().getString("leftBtnName");
		
		rightBtnName = getArguments().getString("rightBtnName");
	}

	@Override
	public void onStart() {
		super.onStart();
		if (getDialog() == null) {
			return;
		}

		setDialogSize();
		
		getDialog().getWindow().setWindowAnimations(R.style.DialogAnimation);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		
		View v = inflater.inflate(R.layout.view_common_dialog, container);
		title = (TextView) v.findViewById(R.id.tv_title);
		content = (TextView) v.findViewById(R.id.tv_content);
		cancel = (Button) v.findViewById(R.id.btn_left);
		confirm = (Button) v.findViewById(R.id.btn_right);
		
		title.setText(titleText);
		content.setText(contentText);
		cancel.setText(leftBtnName);
		confirm.setText(rightBtnName);
		
		if(!TextUtils.isEmpty(contentText)){
			content.setVisibility(View.VISIBLE);
		}
	
		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(null != buttonOnClickListener){
					buttonOnClickListener.cancelButtonClick();
				}
				getDialog().dismiss();
			}
		});
		confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				buttonOnClickListener = (onDialogButtonClickListener) getActivity();
				if(null != buttonOnClickListener){
					buttonOnClickListener.confrimButtonClcik();
				}
				getDialog().dismiss();
			}
		});
		return v;
	}
	
	public void setOnButtonClickListener(onDialogButtonClickListener listener){
		buttonOnClickListener = listener;
	}
	
	private void setDialogSize(){
		Window dialogWindow = getDialog().getWindow();
		dialogWindow.setGravity(Gravity.CENTER);
		
		WindowManager w = getActivity().getWindowManager();
		DisplayMetrics dm= new DisplayMetrics();
		w.getDefaultDisplay().getMetrics(dm);
		
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		if(dm.widthPixels < dm.heightPixels){
			width = (int) (dm.widthPixels * 0.75);
			height = (int) (dm.heightPixels * 0.25);
		}
		lp.width = width;
		lp.height = height;
		lp.alpha = 0.85f;
		dialogWindow.setAttributes(lp);
	}
}
